package com.yunqiao.common.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmsUtils {

    public static void main(String[] args) throws ClientException {
        //Boolean aBoolean = sendBatch(new String[]{"15271601994", "18530881057"}, TemplateEnum.TEMPLATE_8);
        //sendUser("15271601994", TemplateEnum.TEMPLATE_7);

        List<String> list = new ArrayList<>();
        list.add("15271601994");
        sendBatch(list, TemplateEnum.TEMPLATE_8);
    }

    /**
     * 单条发送
     * @param phone
     * @param templateEnum
     * @param code
     * @return
     * @throws ClientException
     */
    public static Boolean send(String phone, TemplateEnum templateEnum, String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIImGKX31GCgyZ", "hHfRnIxtZMMUBZLf108U5IGX8EmWVD");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        //写死  版本
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "今日速运科技");
        request.putQueryParameter("TemplateCode", templateEnum.getValue());
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");

        CommonResponse response = client.getCommonResponse(request);
        Console.log(response);
        Console.log(response.getData());
        JSONObject jsonObject = JSONUtil.parseObj(response.getData());
        if (jsonObject.getStr("Code").equals("OK")) {
            return true;
        }
        return false;
    }

    /**
     * 单条发送   通知代理商账号密码
     * @param phone
     * @return
     * @throws ClientException
     */
    public static Boolean sendPartner(String phone) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIImGKX31GCgyZ", "hHfRnIxtZMMUBZLf108U5IGX8EmWVD");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        //写死  版本
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "今日速运科技");
        request.putQueryParameter("TemplateCode", TemplateEnum.TEMPLATE_9.getValue());
        Dict dict = Dict.create().set("account", phone).set("password", "admin");
        request.putQueryParameter("TemplateParam", JSONUtil.toJsonStr(dict));
        CommonResponse response = client.getCommonResponse(request);
        Console.log(response);
        Console.log(response.getData());
        JSONObject jsonObject = JSONUtil.parseObj(response.getData());
        if (jsonObject.getStr("Code").equals("OK")) {
            return true;
        }
        return false;
    }
    /**
     * 发送  通知骑手
     * @param phone list
     * @param templateEnum
     * @return
     * @throws ClientException
     */
    public static Boolean sendBatch(List<String> phone, TemplateEnum templateEnum) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIImGKX31GCgyZ", "hHfRnIxtZMMUBZLf108U5IGX8EmWVD");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        //写死  版本
        request.setVersion("2017-05-25");
        request.setAction("SendBatchSms");
        request.putQueryParameter("PhoneNumberJson", JSONUtil.toJsonStr(phone));
        List<String> SignNameJson = new ArrayList<>();
        List<Map<String, String>>  TemplateParamJson = new ArrayList<>();
        int size = phone.size();
        for (int i = 0;i< size;i++) {
            SignNameJson.add("今日速运科技");
            Map<String, String> map = new HashMap<>();
            map.put("name", "有用户");
            TemplateParamJson.add(map);
        }
        request.putQueryParameter("SignNameJson", JSONUtil.toJsonStr(SignNameJson));
        request.putQueryParameter("TemplateCode", templateEnum.getValue());

        request.putQueryParameter("TemplateParamJson", JSONUtil.toJsonStr(TemplateParamJson));

        CommonResponse response = client.getCommonResponse(request);
        Console.log(response);
        Console.log(response.getData());
        JSONObject jsonObject = JSONUtil.parseObj(response.getData());
        if (jsonObject.getStr("Code").equals("OK")) {
            return true;
        }
        return false;




    }

    /**
     * 用户订单已被接单 ， 通知用户
     * @param phone
     * @param templateEnum
     * @throws ClientException
     */
    public static Boolean sendUser(String phone, TemplateEnum templateEnum) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIImGKX31GCgyZ", "hHfRnIxtZMMUBZLf108U5IGX8EmWVD");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        //写死  版本
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "今日速运科技");
        request.putQueryParameter("TemplateCode", templateEnum.getValue());

        CommonResponse response = client.getCommonResponse(request);
        Console.log(response);
        Console.log(response.getData());
        JSONObject jsonObject = JSONUtil.parseObj(response.getData());
        if (jsonObject.getStr("Code").equals("OK")) {
            return true;
        }
        return false;
    }

    /**
     * 模板选择
     */
    public enum TemplateEnum {
        /**
         * 身份验证验证码
         */
        TEMPLATE_1("SMS_163005058"),
        /**
         * 登录确认验证码
         */
        TEMPLATE_2("SMS_163005057"),
        /**
         * 登录异常验证码
         */
        TEMPLATE_3("SMS_163005056"),
        /**
         * 用户注册验证码
         */
        TEMPLATE_4("SMS_163005055"),
        /**
         * 修改密码验证码
         */
        TEMPLATE_5("SMS_163005054"),
        /**
         * 信息变更验证码
         */
        TEMPLATE_6("SMS_163005053"),
        /**
         * 订单被骑手接单 通知下单人
         */
        TEMPLATE_7("SMS_163438806"),
        /**
         * 用户下单， 通知骑手
         */
        TEMPLATE_8("SMS_163438804"),
        /**
         * 用户下单， 通知骑手
         */
        TEMPLATE_9("SMS_164269989")

        ;

        private String value;

        TemplateEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }
}
